package p3.entity;

import p3.util.Tool;

import java.util.Date;
import java.util.Scanner;

public class ParkingSystem {
    public static void main(String[] args) {
        ParkingArea area=new ParkingArea();
        area.init();
        boolean flag=true;
        while (true) {
            System.out.println("****欢迎进入停车系统****");
            System.out.println("目前剩余车位："+area.getFreeLots());
            System.out.println("****1.请输入你的车牌****");
            System.out.println("****2.管理员****");
            System.out.println("****3.退出****");
            Scanner scanner = new Scanner(System.in);
            int select = scanner.nextInt();
            switch (select) {
                case 1:
                    System.out.println("请输入你的车牌号:");
                    String carNo = scanner.next();
                    Car car=area.findCarByCarNo(carNo);
                    if (car==null) {
                        car = new Car();
                        car.setCarNo(carNo);
                        System.out.println("车牌登记成功！");
                    }else {
                        System.out.println(carNo+"已经停在车位上");
                    }
                    while (true) {
                        System.out.println("欢迎车牌为" + car.getCarNo() + "的车主，请选择你需要的服务");
                        System.out.println("1.停车");
                        System.out.println("2.离开并缴费");
                        System.out.println("3.查看停车车位");
                        System.out.println("4.停车时长");
                        System.out.println("0.返回上一级");
                        int select2 = scanner.nextInt();
                        if (select2 == 0) {
                            break;
                        } else if (select2 == 1) {
                            if (area.getFreeLots()==0){
                                System.out.println("抱歉，车位已满，不能再此停车");
                            }else {
                                car.parking(area);
                                System.out.println("停车成功，停车位置为：" + area.getLots()[car.getIndex()].getLotNo());
                            }
                        } else {
                            if (car.getIndex() == -1) {
                                System.out.println("请先选择车位停车~");
                            } else {
                                if (select2 == 2) {
                                    System.out.println("停车时间:" + Tool.dateFormat(car.getStartTime()));
                                    System.out.println("当前时间：" + Tool.dateFormat(new Date()));
                                    System.out.println("已经停车时间:" + Tool.getSecond(car.getStartTime(), new Date()));
                                    System.out.println("停车费为：" + car.cost(area) + "元");
                                } else if (select2 == 3) {

                                    System.out.println("您的爱车停在：" + area.getLots()[car.getIndex()].getLotNo());
                                } else if (select2 == 4) {
                                    Date currentTime = new Date();
                                    int time = Tool.getSecond(car.getStartTime(), currentTime);
                                    System.out.println("停车时长为：" + time + "秒");
                                }
                            }
                        }


                    }
                    break;
                case 2:
                    while (true){
                        System.out.println("1.管理员登录");
                        System.out.println("2.管理员注册");
                        System.out.println("3.退出");
                        int select2=scanner.nextInt();
                        if (select2==3){
                            break;
                        }
                        Admin admin=new Admin();
                        if (select2==1){
                          Boolean b=  admin.login(area);
                          if (b){
                              System.out.println("登录成功");
                              while (true) {
                                  System.out.println("**欢迎进入管理员操作***");
                                  System.out.println("1.查看现有停车位");
                                  System.out.println("2.查看停车位上的车以及缴费");
                                  System.out.println("3.返回上一级");
                                  int select3=scanner.nextInt();
                                  if (select3==3){
                                      break;
                                  }else if (select3==1){
                                      admin.show(area);
                                  }else {
                                      admin.showCost(area);
                                  }
                              }
                          }else {
                              System.out.println("登陆失败");
                          }
                        }else {

                            admin.register(area);
                        }
                    }
                    break;
                case 3:
                    System.out.println("欢迎下次光临");
                    flag=false;
                    break;

            }

            if (!flag){
                break;
            }
        }
    }
}
